package com.songtech.store.controller;

import com.github.pagehelper.PageInfo;
import com.songtech.auth.user.model.User;
import com.songtech.base.common.BaseConstant;
import com.songtech.base.common.JsonResult;
import com.songtech.device.service.DeviceService;
import com.songtech.shiro.annotation.UserInfo;
import com.songtech.store.bean.Store;
import com.songtech.store.bean.StoreVo;
import com.songtech.store.service.StoreService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: RENTIE
 * Date: 2018-06-19 14:35
 */
@RestController
@RequestMapping("/store")
public class StoreController {
    private final Logger logger = LoggerFactory.getLogger(StoreController.class);

    @Autowired
    private StoreService storeService;

    @Autowired
    private DeviceService deviceService;


    /**
     * 入库
     * @param bean
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public JsonResult add(@UserInfo User user, Store bean){
        Integer count=0;
        bean.setInTime(new Date());
        bean.setInPerson(user.getLoginName());
        try {
           count= storeService.insertSelective(bean);

        } catch (Exception e) {
            logger.error("入库异常信息:"+e.getMessage(),e);
            return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }


    /**
     * 出库
     * @param bean
     * @return
     */
    @RequestMapping(value = "/reduce",method = RequestMethod.POST)
    public JsonResult reduce(@UserInfo User user, Store bean){

        Integer count=0;
        bean.setOutTime(new Date());
        bean.setStatus("1");
        try {
            count= storeService.updateByImeiSelective(bean);

        } catch (Exception e) {
            logger.error("出库异常信息:"+e.getMessage(),e);
            return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }

    /**
     * 入库管理查询
     * @param paramMap  查询条件封装map
     *           companyCode 厂商编号
     *           productCode 产品名称编号
     *           modelNumber 产品型号
     *           page（当前页）
     *           limit（每页个数）
     *            imei（终端标识码）
     *            storeRegion(库房位置)
     *            startTime（条件开始时间）
     *            endTime （条件结束时间）
     * @return
     */
    @RequestMapping(value = "/queryin",method = RequestMethod.GET)
    public JsonResult queryIn(@RequestParam Map<String,String> paramMap){
        PageInfo<Store> list=null;
        try {
            list= storeService.selectInStoreList(paramMap);
        } catch (Exception e) {
            logger.error("入库管理查询异常信息:"+e.getMessage(),e);
            return new JsonResult(null, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(list.getList(), BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS,list.getTotal());
    }

    /**
     * 出库管理查询
     * @param paramMap  查询条件封装map
     *           companyCode 厂商编号
     *           productCode 产品名称编号
     *           modelNumber 产品型号
     *           page（当前页）
     *           limit（每页个数）
     *            imei（终端标识码）
     *            storeRegion(库房位置)
     *            startTime（条件开始时间）
     *            endTime （条件结束时间）
     * @return
     */
    @RequestMapping(value = "/queryout",method = RequestMethod.GET)
    public JsonResult queryOut(@RequestParam Map<String,String> paramMap){
        PageInfo<Store> list=null;
        try {
           list= storeService.selectOutStoreList(paramMap);

        } catch (Exception e) {
            logger.error("出库管理查询异常信息:"+e.getMessage(),e);
            return new JsonResult(null, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(list.getList(), BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS,list.getTotal());
    }

    /**
     * 更新出入库管理信息
     * @param bean
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public JsonResult update(Store bean){

        Integer count=0;
        try {
            count= storeService.updateByPrimaryKeySelective(bean);

        } catch (Exception e) {
            logger.error("出入库管理修改异常信息:"+e.getMessage(),e);
            return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }

    /**
     * 获取出入库管理详情
     * @param id 数据id
     * @return
     */
    @RequestMapping(value = "/datail",method = RequestMethod.GET)
    public JsonResult detail(Long id){
        Store bean=null;
        try {
            bean= storeService.selectByPrimaryKey(id);

        } catch (Exception e) {
            logger.error("出入库管理修改异常信息:"+e.getMessage(),e);
            return new JsonResult(bean, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(bean, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }

    @RequestMapping(value = "/indelete",method = RequestMethod.POST)
    public JsonResult inDelete(Store bean){
        bean.setIsDelete("1");
        Integer count=0;
        try {
            count= storeService.updateByPrimaryKeySelective(bean);

        } catch (Exception e) {
            logger.error("入库管理删除异常信息:"+e.getMessage(),e);
            return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }

    @RequestMapping(value = "/outdelete",method = RequestMethod.POST)
    public JsonResult outDelete(Store bean){
        bean.setStatus("0");
        Integer count=0;
        try {
            count= storeService.updateByPrimaryKeySelective(bean);

        } catch (Exception e) {
            logger.error("出库管理删除异常信息:"+e.getMessage(),e);
            return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }



    /**
     * 查询设备总量
     * @return
     */
    @RequestMapping(value = "/allDevice",method = RequestMethod.POST)
    public JsonResult allDevice(String companyCode,String productCode,String modelNumber){
        Map<String,String> map=new HashMap<>();
        map.put("companyCode",companyCode);
        map.put("productCode",productCode);
        map.put("modelNumber",modelNumber);
        int i=0;
        try {
            i= storeService.selectAllDevice(map);
        } catch (Exception e) {
            logger.error("设备总量查询异常信息:"+e.getMessage(),e);
            return new JsonResult(i, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(i, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);

    }

    /**
     * 终端列表详情
     * @return
     */
    @RequestMapping(value = "/deviceList",method = RequestMethod.GET)
    public JsonResult deviceList(@RequestParam Map<String,String> map){
        PageInfo<StoreVo> list=null;
        try {
            list=storeService.deviceList(map);
        } catch (Exception e) {
            logger.error("终端列表详情查询异常信息:"+e.getMessage(),e);
            return new JsonResult(null, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(list.getList(), BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS,list.getTotal());
    }

    /**
     * 设备已卖出量
     * @param companyCode
     * @param productCode
     * @param modelNumber
     * @return
     */
    @RequestMapping(value = "/deviceOut",method = RequestMethod.POST)
    public JsonResult deviceOut(String companyCode,String productCode,String modelNumber){
        Map<String,String> map=new HashMap<>();
        map.put("companyCode",companyCode);
        map.put("productCode",productCode);
        map.put("modelNumber",modelNumber);
        int i=0;
        try {
            i=storeService.deviceOut(map);
        } catch (Exception e) {
            logger.error("设备已卖出量查询异常信息:"+e.getMessage(),e);
            return new JsonResult(i, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(i, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }

    /**
     * 今日设备已卖出量
     * @param companyCode
     * @param productCode
     * @param modelNumber
     * @return
     */
    @RequestMapping(value = "/deviceTodayOut",method = RequestMethod.POST)
    public JsonResult deviceTodayOut(String companyCode,String productCode,String modelNumber){
        Map<String,String> map=new HashMap<>();
        map.put("companyCode",companyCode);
        map.put("productCode",productCode);
        map.put("modelNumber",modelNumber);
        int i=0;
        try {
            i=storeService.deviceTodayOut(map);
        } catch (Exception e) {
            logger.error("设备已卖出量查询异常信息:"+e.getMessage(),e);
            return new JsonResult(i, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(i, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);

    }

    /**
     * 查询设备库存量
     * @param companyCode
     * @param productCode
     * @param modelNumber
     * @return
     */
    @RequestMapping(value = "/deviceInventory",method = RequestMethod.POST)
    public JsonResult deviceInventory(String companyCode,String productCode,String modelNumber){
        Map<String,String> map=new HashMap<>();
        map.put("companyCode",companyCode);
        map.put("productCode",productCode);
        map.put("modelNumber",modelNumber);
        Integer i=0;
        try {
            i= storeService.deviceInventory(map);
        } catch (Exception e) {
            logger.error("设备库存量查询异常信息:"+e.getMessage(),e);
            return new JsonResult(i, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(i, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }



    @RequestMapping(value="/diviceNum",method = RequestMethod.GET)
    public JsonResult diviceNum(String companyCode,String productCode,String modelNumber){
        LinkedHashMap<String,Object> hashMap = null;
        try{
            Map<String,String> map  = new HashMap<>();
            map.put("companyCode",companyCode);
            map.put("productCode",productCode);
            map.put("modelNumber",modelNumber);

            hashMap = new LinkedHashMap<>();
            //全部设备
            int i=storeService.selectAllDevice(map);
            hashMap.put("allDevice",i);
            //今日卖出
            hashMap.put("deviceTodayOut",storeService.deviceTodayOut(map));
            //已卖设备量
            hashMap.put("deviceOut",storeService.deviceOut(map));
            //在线设备量
            hashMap.put("deviceOnline",deviceService.deviceOnlineCount(map));
        }catch (Exception e){
            logger.error("统计设备销量和使用量异常:"+e.getMessage(),e);
            return new JsonResult(hashMap, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(hashMap, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }


    /**
     * 查询设备库存、已卖出、在线、离线数量
     * @param companyCode
     * @param productCode
     * @param modelNumber
     * @return
     */
    @RequestMapping(value="/deviceCount",method = RequestMethod.GET)
    public JsonResult deviceCount(String companyCode,String productCode,String modelNumber){
        LinkedHashMap<String,Object> hashMap = null;
        try{
            Map<String,String> map  = new HashMap<>();
            map.put("companyCode",companyCode);
            map.put("productCode",productCode);
            map.put("modelNumber",modelNumber);

            hashMap = new LinkedHashMap<>();
            //已卖设备量
            hashMap.put("deviceOut",storeService.deviceOut(map));
            //库存设备量
            hashMap.put("inventory",storeService.deviceInventory(map));
            //在线设备量
            hashMap.put("deviceOnline",deviceService.deviceOnlineCount(map));
            //离线设备量
            hashMap.put("deviceOffline", deviceService.deviceOfflineCount(map));
        }catch (Exception e){
            logger.error("统计设备销量和使用量异常:"+e.getMessage(),e);
            return new JsonResult(hashMap, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(hashMap, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }

    /**
     * 终端设备趋势
     * @param map
     * @return
     */
    @RequestMapping(value="deviceChatLine",method = RequestMethod.GET)
    public JsonResult deviceChatLine(@RequestParam Map<String,Object> map){
        LinkedHashMap<String,Object> hashMap = null;
        try{
            hashMap = new LinkedHashMap<>();
            hashMap.put("deviceOutList",storeService.deviceOutByDay(map));
            hashMap.put("deviceAllList",storeService.deviceAllByDay(map));
            hashMap.put("deviceOnlineList",deviceService.deviceOnlineByDay(map));
        }catch (Exception e){
            logger.error("查询设备卖出记录、总设备记录、在线设备记录异常:"+e.getMessage(),e);
            return new JsonResult(hashMap, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(hashMap, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }

    /**
     * 查询各个地区设备卖出数和在线数
     * @return
     */
    @RequestMapping(value="deviceCountByProvince",method = RequestMethod.GET)
    public JsonResult queryDeviceCountByProvince(@RequestParam Map<String,String> map){
        Map<String,Object> hashMap = null;
        try{
            hashMap = storeService.deviceByProvince(map);
        }catch (Exception e){
            logger.error("根据地区查询设备总量和在线数异常:"+e.getMessage(),e);
            return new JsonResult(hashMap, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
        }
        return new JsonResult(hashMap, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
    }

}
